package ru.amse.tsyganov.jumleditor.main;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import ru.amse.tsyganov.jumleditor.view.preferences.Images;

public class ErrorMessage extends Dialog {

	private static final Point NORMAL_SIZE = new Point(350, 100);
	
	private static final Point DETAILS_SIZE = new Point(350, 325);
	
	private Shell sShell = null;  //  @jve:decl-index=0:visual-constraint="10,10"
	private Label imageLabel = null;
	private Label messageLabel = null;
	private Button closeButton = null;
	private Button detailsButton = null;
	private Text detailsTextArea = null;
	
	private boolean isDetail = false;
	
	public ErrorMessage(Shell parent, String message, String details) {
		super(parent);
		createSShell();
		sShell.setLocation(
				getParent().getLocation().x + 
				getParent().getSize().x / 2 - 
				sShell.getSize().x / 2, 
				getParent().getLocation().y + 
				getParent().getSize().y / 2 - 
				DETAILS_SIZE.y / 2
				);
		messageLabel.setText(message);
		detailsTextArea.setText(details);
	}
	
	/**
	 * This method initializes sShell
	 */
	private void createSShell() {
		sShell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		sShell.setText("JUmlEditor");
		sShell.setLayout(null);
		sShell.setSize(NORMAL_SIZE);
		sShell.setImage(Images.getErrorImage());
		imageLabel = new Label(sShell, SWT.NONE | SWT.BORDER);
		imageLabel.setImage(Images.getBigErrorImage());
		imageLabel.setBounds(new Rectangle(10, 10, 34, 34));
		messageLabel = new Label(sShell, SWT.NONE);
		messageLabel.setBounds(new Rectangle(60, 10, 190, 60));
		closeButton = new Button(sShell, SWT.NONE);
		closeButton.setBounds(new Rectangle(260, 10, 70, 23));
		closeButton.setText("Close");
		detailsButton = new Button(sShell, SWT.NONE);
		detailsButton.setBounds(new Rectangle(260, 40, 70, 23));
		detailsButton.setText("Details>>");
		detailsTextArea = new Text(sShell, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL | SWT.BORDER | SWT.H_SCROLL);
		detailsTextArea.setBounds(new Rectangle(10, 80, 320, 200));
		detailsTextArea.setEditable(false);
		
		detailsButton.addSelectionListener(new SelectionListener() {

			public void widgetDefaultSelected(SelectionEvent e) {
				widgetSelected(e);
			}

			public void widgetSelected(SelectionEvent e) {
				if (isDetail) {
					sShell.setSize(NORMAL_SIZE);
					detailsButton.setText("Details>>");
				} else {
					sShell.setSize(DETAILS_SIZE);
					detailsButton.setText("Details<<");
				}
				isDetail = !isDetail;
			}
			
		});
		
		closeButton.addSelectionListener(new SelectionListener() {

			public void widgetDefaultSelected(SelectionEvent e) {
				widgetSelected(e);
			}

			public void widgetSelected(SelectionEvent e) {
				sShell.close();
				sShell.dispose();
			}
			
		});
		
		closeButton.setFocus();
	}

	public void open() {
		 sShell.open();
		 Display display = getParent().getDisplay();
		 while (!sShell.isDisposed()) {
		 	if (!display.readAndDispatch()) display.sleep();
		 }
	}
	
	public static String getTextFormException(Exception e) {
		OutputStream os = new ByteArrayOutputStream();
		e.printStackTrace(new PrintStream(os));
		return os.toString();
	}
}
